<?php
/**
 * @title 1670. 设计前中后队列
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class FrontMiddleBackQueue {
    /**
     */
    function __construct() {
        /**
         * @since 2023-11-28 全局队列
         */
        $this->queueArray = [];
    }

    /**
     * @param Integer $val
     * @return NULL
     */
    function pushFront($val) {
        array_unshift($this->queueArray, $val);
    }

    /**
     * @param Integer $val
     * @return NULL
     */
    function pushMiddle($val) {
        /**
         * @since 2023-11-28 队列长度
         */
        $len = count($this->queueArray);

        /**
         * @since 2023-11-28 切割成前后2个数组
         */
        $len1 = $len>>1;
        $arr1 = array_slice($this->queueArray, 0, $len1);
        $arr2 = array_slice($this->queueArray, $len1);

        /**
         * @since 2023-11-28 合并数组
         */
        $this->queueArray = array_merge($arr1, [$val], $arr2);
    }

    /**
     * @param Integer $val
     * @return NULL
     */
    function pushBack($val) {
        $this->queueArray[] = $val;
    }

    /**
     * @return Integer
     */
    function popFront() {
        /**
         * @since 2023-11-28 移出队头，并返回值
         */
        $num = array_shift($this->queueArray);
        if($num === null){
            $num = -1;
        } else {}

        /**
         * @return
         */
        return $num;
    }

    /**
     * @return Integer
     */
    function popMiddle() {
        /**
         * @since 2023-11-28 队列长度
         */
        $len = count($this->queueArray);

        /**
         * @since 2023-11-28 空队列
         */
        if($len == 0){
            return -1;
        } else {}

        /**
         * @since 2023-11-28 移出中间键值
         */
        $middleKey = ($len-1)>>1;
        $num = $this->queueArray[$middleKey];
        unset($this->queueArray[$middleKey]);

        /**
         * @since 2023-11-28 重排键值
         */
        $this->queueArray = array_values($this->queueArray);

        /**
         * @return
         */
        return $num;
    }

    /**
     * @return Integer
     */
    function popBack() {
        /**
         * @since 2023-11-28 移出队尾，并返回值
         */
        $num = array_pop($this->queueArray);
        if($num === null){
            $num = -1;
        } else {}

        /**
         * @return
         */
        return $num;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202311/2023.11.28.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
